package kata09.shop;

import kata09.customer.ShoppingCar;
import kata09.info.Goods;
import kata09.order.Order;
import lombok.Data;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

//商店
@Data
public class Shop {

    //hash 商品仓库(上架商品)
    private Map<String,Goods> repository;
    //购物车
    private ShoppingCar shoppingCar;

    public Shop(){
        repository = new HashMap<>();
        shoppingCar = new ShoppingCar();
    }

    //添加商品(上架商品)
    public void  addGoods(Goods goods){
        repository.put(goods.getGoods().getName(),goods);
    }

    //商品加入购物车
    public void addShopCar(String goods){
        char[] chars = goods.toCharArray();
        Goods exists;
        for (char cr : chars){
            //检查商品是否存在
            exists = repository.get(String.valueOf(cr));
            if(exists != null){
                shoppingCar.addShoppingCar(exists,1);
            }
        }
    }

    //结算购物车商品
    public BigDecimal price(){
        Order order = shoppingCar.generateOrder();
        if(order != null){
            //返回折后价格
            return order.getDiscountCountPrice();
        }
        return BigDecimal.ZERO;
    }
}
